#!/usr/bin/env python
#coding:utf-8
#
# Copyright 2009 CoderZh.com.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = 'CoderZh'

import logging
from google.appengine.ext import db
from google.appengine.api import users

class Category(db.Model):
    """Category Model
    """
    name = db.StringProperty()
    owner = db.UserProperty()
    parent_category = db.ReferenceProperty()
    description = db.TextProperty()
    addtime = db.TimeProperty(auto_now_add=True)
    enable = db.BooleanProperty(default=True)

    @staticmethod
    def get_by_current_user(id):
        item = Category.get_by_id(int(id))
        if item and item.owner != users.get_by_current_user():
            return None
        return item

    @staticmethod
    def create(name, description, parent=None):
        """create a category
        
        Arguments:
        - `name`:
        - `description`:
        - `parent`:
        """
        user = users.get_current_user()
        new_category = Category(name=name, description=description, parent_category=parent, owner=user)
        new_category.put()

    @staticmethod
    def delete(id):
        """delete a category
        
        Arguments:
        - `id`:
        """
        c_id = int(id)
        item = Category.get_by_id(c_id)
        if item:
            if item.owner != users.get_current_user():
                raise "You can not delete other's Category"
            item.enable = False
            item.put()
        else:
            raise 'Category not found!'

    @staticmethod
    def edit(id, name, description, parent=None, enable=True):
        """edit a category
        
        Arguments:
        - `id`:
        - `name`:
        - `description`:
        - `parent`:
        - `enable`:
        """
        c_id = int(id)
        item = Category.get_by_id(c_id)
        if item:
            if item.owner != users.get_current_user():
                raise "You can not edit other's Category"
            item.name = name
            item.description = description
            item.parent_category = parent
            item.enable = enable
            item.put()


class Shortcuts(db.Model):
    """Shortcuts Models
    """
    name = db.StringProperty()
    link = db.LinkProperty()
    owner = db.UserProperty()
    addtime = db.TimeProperty(auto_now_add=True)
    logo = db.LinkProperty()
    category = db.ReferenceProperty(reference_class=Category)
    left = db.IntegerProperty()
    top = db.IntegerProperty()
    enable = db.BooleanProperty(default=True)

    @staticmethod
    def get_by_current_user(id):
        item = Shortcuts.get_by_id(int(id))
        if item and item.owner != users.get_current_user():
            return None
        return item

    @staticmethod
    def get_all_by_current_user():
        return Shortcuts.all().filter('owner =', users.get_current_user()).filter('enable =', True).fetch(50)
    
    @staticmethod
    def create(name, link, logo, category):
        """create a new shortcut
        
        Arguments:
        - `name`:
        - `link`:
        - `logo`:
        - `category`:
        """
        user = users.get_current_user()
        new_shortcut = Shortcuts(name=name, link=link, owner=user, logo=logo, category=category)
        new_shortcut.put()

    @staticmethod
    def create(link):
        """quick create a new shortcut
        
        Arguments:
        - `link`:
        """
        user = users.get_current_user()
        name = Shortcuts.get_link_name(link)
#        logo = Shortcuts.get_link_logo(link)
        new_shortcut = Shortcuts(name=name, link=link, owner=user)
        new_shortcut.put()
        return new_shortcut

    @staticmethod
    def delete(id):
        """delete a shortcut by id
        
        Arguments:
        - `id`:
        """
        s_id = int(id)
        item = Shortcuts.get_by_id(s_id)
        if item:
            if item.owner != users.get_current_user():
                raise "You can not delete other's Shortcut"
            item.enable = False
            item.put()
        else:
            raise 'Shortcut not found'

    @staticmethod
    def edit(id, name, link, logo, category):
        """edit a shortcut
        
        Arguments:
        - `id`:
        - `name`:
        - `link`:
        - `logo`:
        - `category`:
        """
        s_id = int(id)
        item = Shortcuts.get_by_id(s_id)
        if item:
            if item.owner != users.get_current_user():
                raise "You can not edit other's Shortcut"
            item.name = name
            item.link = link
            item.logo = logo
            item.category = category
            item.put()

    @staticmethod
    def get_link_name(link):
        return link.lstrip('http://')

    @staticmethod
    def move(id, left, top):
        item = Shortcuts.get_by_id(int(id))
        if item:
            if item.owner != users.get_current_user():
                raise "You can not move other's Shortcut"
            item.left = int(left.rstrip('px').rstrip('pt'))
            item.top = int(top.rstrip('px').rstrip('pt'))
            item.put()
